package es.ats.codington.web.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorServiceImpl;

/**
 * Servlet implementation class RegisterVisitorServlet.
 */
@WebServlet("/RegisterVisitorServlet")
public class RegisterVisitorServlet extends HttpServlet {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
       
    /**
     * Instantiates a new register visitor servlet.
     *
     * @see HttpServlet#HttpServlet()
     */
    public RegisterVisitorServlet() {
        super();
    }

	/**
	 * Do get.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * Do post.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
            VisitorServiceImpl vis = new VisitorServiceImpl();
                     
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String name = request.getParameter("name");
            String lastname = request.getParameter("lastname");
            String age = request.getParameter("age");
            String address = request.getParameter("address");
            String phone = request.getParameter("phone");
            
            if ((vis.searchVisitorByUsername(username)).getVisitorId()==0){
            	HttpSession session = request.getSession(true);
            	
            	Visitor visitor = new Visitor(0, name, lastname, 
                		Integer.parseInt(age), address, username, 
            			password, phone, false);
                
                vis.registerVisitor(visitor);
                visitor = vis.searchVisitorByUsername(username);           
                session.setAttribute("VisitorID", visitor.getVisitorId());
                session.setAttribute("Session", -1);
                Cookie loginCookie = new Cookie("visitor",username);
                
                //setting cookie to expiry in 30 mins
                //loginCookie.setMaxAge(30*60);
                
               /* response.addCookie(loginCookie);
                response.sendRedirect("ListEventServlet");*/
                session.setAttribute("Session", 6);
                ListEventServlet list = new ListEventServlet();
                list.sendListEventvisitor(request, response);
            }else{
            	String errorMessage = "The username is used";
            	Cookie registerCookie = new Cookie("message",errorMessage);
            	Cookie loginCookie = new Cookie("visitor", username);
            	response.addCookie(loginCookie);
            	response.addCookie(registerCookie);
            	RequestDispatcher rd=request.getRequestDispatcher("registerVisitor.jsp");  
                rd.forward(request,response); 
            }
        }catch(Exception theException){
             System.out.println(theException); 
        }
    }
}
